package fr.alma.domo.svc;

import org.apache.tuscany.sca.node.Contribution;
import org.apache.tuscany.sca.node.ContributionLocationHelper;
import org.apache.tuscany.sca.node.Node;
import org.apache.tuscany.sca.node.NodeFactory;

public class ServiceFactory {

	private static AdaptationHandler ADAPTATION_HANDLER;
	private static ContextHandler CONTEXT_HANDLER;
	private static SelectionAndDiscovery SELECTION_AND_DISCOVERY;
	private static CollaborationHandler COLLABORATION_HANDLER;
	private static Node _NODE;
	
	private static void initNode(){
		if(_NODE==null){
			String loc = ContributionLocationHelper.getContributionLocation(ServiceFactory.class);
			_NODE = NodeFactory.newInstance().createNode(new Contribution("c1",loc));
			_NODE.start();
		}
	}
		
	public static AdaptationHandler getAdaptationHandler(){
		if(ADAPTATION_HANDLER==null){
			initNode();
			ADAPTATION_HANDLER = _NODE.getService(AdaptationHandler.class, "AdaptationHandlerComponent");
		}
		return ADAPTATION_HANDLER;
	}
	
	public static ContextHandler getContextHandler(){
		if(CONTEXT_HANDLER==null){
			initNode();
			CONTEXT_HANDLER = _NODE.getService(ContextHandler.class, "ContextHandlerComponent");
		}
		return CONTEXT_HANDLER;
	}
	
	public static SelectionAndDiscovery getSelectionAndDiscovery(){
		if(SELECTION_AND_DISCOVERY==null){
			initNode();
			SELECTION_AND_DISCOVERY = _NODE.getService(SelectionAndDiscovery.class, "SelectionAndDiscoveryComponent");
		}
		return SELECTION_AND_DISCOVERY;
	}
	
	public static CollaborationHandler getCollaborationHandler(){
		if(COLLABORATION_HANDLER==null){
			initNode();
			COLLABORATION_HANDLER = _NODE.getService(CollaborationHandler.class, "CollaborationHandlerComponent");
		}
		return COLLABORATION_HANDLER;
	}
}
